package codeService

import (
	"errors"
	"github.com/spf13/cast"
	"hc_server/routers/models"
)

type Code struct {
	Mobile interface{}
	Code   interface{}
	Ip     interface{}
	Type   int16
}

// ExistCodeByMobile 通过mobile判断是否存在
func (c *Code) ExistCodeByMobile() (bool, error) {
	return models.ExistCodeByMobile(cast.ToString(c.Code), cast.ToString(c.Mobile))
}

// UpdateCodeIsCheck 更新验证码状态
func (c *Code) UpdateCodeIsCheck() error {
	return models.UpdateCodeIsCheck(cast.ToString(c.Code))
}

// GetCodeCount 获取验证码次数
func (c *Code) GetCodeCount() error {
	count := models.GetCodeCount(cast.ToString(c.Mobile), c.Type)
	if count >= 3 {
		return errors.New("操作频繁，请稍后再试")
	}
	return nil
}

// AddCode 添加验证码
func (c *Code) AddCode() error {
	err := models.AddCode(cast.ToString(c.Ip), cast.ToString(c.Mobile), cast.ToString(c.Code), cast.ToInt16(c.Type))
	if err != nil {
		return errors.New("验证码发送失败")
	}
	return nil
}
